package com.qf.route;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //① 创建一个连接工厂对象
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("cdm");

        //② 利用连接工厂获取一个连接对象
        Connection connection = factory.newConnection();

        //③ 利用连接对象获取一个channel对象
        Channel channel = connection.createChannel();

        //④ 消费消息
        channel.basicConsume("routeQue1", true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者标记:" + consumerTag);
                System.out.println("msg:" + new String(body,"utf-8"));
            }
        });
    }
}
